package com.epam.edu.task01.tourism.voucher;

import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

import com.epam.edu.task01.tourism.Program;
import com.epam.edu.task01.tourism.residence.*;
import com.epam.edu.task01.tourism.road.*;

public class VouchersList {

	private static Logger logger = Logger.getLogger(Program.class);

	private List<VoucherBuilder> vouchers;

	@Override
	public String toString() {
		String voucherList = "";
		for (VoucherBuilder str : this.vouchers) {
			voucherList.concat(str.toString() + "\n");
		}
		return voucherList;
	}

	public void addVoucherToCruise(String... datas) throws ParseException {
		BasicConfigurator.configure();
		if (datas.length != 8) {
			logger.error("Insufficient data");
			return;
		}
		Transport transportType = null;
		switch (datas[5]) {
		case "plane": {
			transportType = Transport.PLANE;
			break;
		}
		case "train": {
			transportType = Transport.TRAIN;
			break;
		}
		case "bus": {
			transportType = Transport.BUS;
			break;
		}
		}
		Route routeType = null;
		switch (datas[6]) {
		case "linear": {
			routeType = Route.LINEAR;
			break;
		}
		case "circular": {
			routeType = Route.CIRCULAR;
			break;
		}
		case "radial": {
			routeType = Route.RADIAL;
			break;
		}
		case "combined": {
			routeType = Route.COMBINED;
			break;
		}
		}

		RoadInformation road = new RoadInformation(transportType, routeType,
				Integer.parseInt(datas[7]));
		DateFormat dateFormat = DateFormat.getDateInstance();
		Date firstDate = (Date) dateFormat.parse(datas[3]);
		Date secondDate = (Date) dateFormat.parse(datas[4]);
		VoucherToCruise voucher = new VoucherToCruise(
				Integer.parseInt(datas[1]), datas[2], firstDate, secondDate,
				road, Integer.parseInt(datas[8]));
		vouchers.add(voucher);
	}

	public void addVoucherToExcursion(String... datas) throws ParseException {
		BasicConfigurator.configure();
		if (datas.length != 9) {
			logger.error("iInsufficient data");
			return;
		}
		Transport transportType = null;
		switch (datas[5]) {
		case "plane": {
			transportType = Transport.PLANE;
			break;
		}
		case "train": {
			transportType = Transport.TRAIN;
			break;
		}
		case "bus": {
			transportType = Transport.BUS;
			break;
		}
		}
		Route routeType = null;
		switch (datas[6]) {
		case "linear": {
			routeType = Route.LINEAR;
			break;
		}
		case "circular": {
			routeType = Route.CIRCULAR;
			break;
		}
		case "radial": {
			routeType = Route.RADIAL;
			break;
		}
		case "combined": {
			routeType = Route.COMBINED;
			break;
		}
		}

		RoadInformation road = new RoadInformation(transportType, routeType,
				Integer.parseInt(datas[7]));
		DateFormat dateFormat = DateFormat.getDateInstance();
		Date date = (Date) dateFormat.parse(datas[4]);
		VoucherToExcursion voucher = new VoucherToExcursion(
				Integer.parseInt(datas[1]), datas[2], datas[3], date, road,
				Integer.parseInt(datas[8]), Integer.parseInt(datas[9]));
		vouchers.add(voucher);
	}

	public void addVoucherToRelax(String... datas) throws ParseException {
		BasicConfigurator.configure();
		if (datas.length != 12) {
			logger.error("iInsufficient data");
			return;
		}

		ResidenceType residenceType = null;
		switch (datas[4]) {
		case "hotel": {
			residenceType = ResidenceType.HOTEL;
			break;
		}
		case "hospital": {
			residenceType = ResidenceType.HOSPITAL;
			break;
		}
		case "apartaments": {
			residenceType = ResidenceType.APARTAMENTS;
			break;
		}
		}

		Nutrition nutrition = null;
		switch (datas[6]) {
		case "bb": {
			nutrition = Nutrition.BB;
			break;
		}
		case "fb": {
			nutrition = Nutrition.FB;
			break;
		}

		}

		DateFormat dateFormat = DateFormat.getDateInstance();
		Date firstDate = (Date) dateFormat.parse(datas[8]);
		Date secondDate = (Date) dateFormat.parse(datas[9]);

		ResidenceInformation residence = new ResidenceInformation(
				residenceType, datas[5], nutrition, Integer.parseInt(datas[7]),
				firstDate, secondDate);

		Transport transportType = null;
		switch (datas[10]) {
		case "plane": {
			transportType = Transport.PLANE;
			break;
		}
		case "train": {
			transportType = Transport.TRAIN;
			break;
		}
		case "bus": {
			transportType = Transport.BUS;
			break;
		}
		}
		Route routeType = null;
		switch (datas[11]) {
		case "linear": {
			routeType = Route.LINEAR;
			break;
		}
		case "circular": {
			routeType = Route.CIRCULAR;
			break;
		}
		case "radial": {
			routeType = Route.RADIAL;
			break;
		}
		case "combined": {
			routeType = Route.COMBINED;
			break;
		}
		}

		RoadInformation road = new RoadInformation(transportType, routeType,
				Integer.parseInt(datas[12]));

		VoucherToRelax voucher = new VoucherToRelax(Integer.parseInt(datas[1]),
				datas[2], datas[3], residence, road);
		vouchers.add(voucher);
	}

	public void addVoucherToShopping(String... datas) throws ParseException {
		BasicConfigurator.configure();
		if (datas.length != 13) {
			logger.error("iInsufficient data");
			return;
		}

		ResidenceType residenceType = null;
		switch (datas[4]) {
		case "hotel": {
			residenceType = ResidenceType.HOTEL;
			break;
		}
		case "hospital": {
			residenceType = ResidenceType.HOSPITAL;
			break;
		}
		case "apartaments": {
			residenceType = ResidenceType.APARTAMENTS;
			break;
		}
		}

		Nutrition nutrition = null;
		switch (datas[6]) {
		case "bb": {
			nutrition = Nutrition.BB;
			break;
		}
		case "fb": {
			nutrition = Nutrition.FB;
			break;
		}

		}

		DateFormat dateFormat = DateFormat.getDateInstance();
		Date firstDate = (Date) dateFormat.parse(datas[8]);
		Date secondDate = (Date) dateFormat.parse(datas[9]);

		ResidenceInformation residence = new ResidenceInformation(
				residenceType, datas[5], nutrition, Integer.parseInt(datas[7]),
				firstDate, secondDate);

		Transport transportType = null;
		switch (datas[10]) {
		case "plane": {
			transportType = Transport.PLANE;
			break;
		}
		case "train": {
			transportType = Transport.TRAIN;
			break;
		}
		case "bus": {
			transportType = Transport.BUS;
			break;
		}
		}
		Route routeType = null;
		switch (datas[11]) {
		case "linear": {
			routeType = Route.LINEAR;
			break;
		}
		case "circular": {
			routeType = Route.CIRCULAR;
			break;
		}
		case "radial": {
			routeType = Route.RADIAL;
			break;
		}
		case "combined": {
			routeType = Route.COMBINED;
			break;
		}
		}

		RoadInformation road = new RoadInformation(transportType, routeType,
				Integer.parseInt(datas[12]));

		VoucherToShopping voucher = new VoucherToShopping(
				Integer.parseInt(datas[1]), datas[2], datas[3], residence,
				road, Integer.parseInt(datas[13]));
		vouchers.add(voucher);
	}

	public void addVoucherToTreatment(String... datas) throws ParseException {
		BasicConfigurator.configure();
		if (datas.length != 13) {
			logger.error("iInsufficient data");
			return;
		}

		ResidenceType residenceType = null;
		switch (datas[4]) {
		case "hotel": {
			residenceType = ResidenceType.HOTEL;
			break;
		}
		case "hospital": {
			residenceType = ResidenceType.HOSPITAL;
			break;
		}
		case "apartaments": {
			residenceType = ResidenceType.APARTAMENTS;
			break;
		}
		}

		Nutrition nutrition = null;
		switch (datas[6]) {
		case "bb": {
			nutrition = Nutrition.BB;
			break;
		}
		case "fb": {
			nutrition = Nutrition.FB;
			break;
		}

		}

		DateFormat dateFormat = DateFormat.getDateInstance();
		Date firstDate = (Date) dateFormat.parse(datas[8]);
		Date secondDate = (Date) dateFormat.parse(datas[9]);

		ResidenceInformation residence = new ResidenceInformation(
				residenceType, datas[5], nutrition, Integer.parseInt(datas[7]),
				firstDate, secondDate);

		Transport transportType = null;
		switch (datas[10]) {
		case "plane": {
			transportType = Transport.PLANE;
			break;
		}
		case "train": {
			transportType = Transport.TRAIN;
			break;
		}
		case "bus": {
			transportType = Transport.BUS;
			break;
		}
		}
		Route routeType = null;
		switch (datas[11]) {
		case "linear": {
			routeType = Route.LINEAR;
			break;
		}
		case "circular": {
			routeType = Route.CIRCULAR;
			break;
		}
		case "radial": {
			routeType = Route.RADIAL;
			break;
		}
		case "combined": {
			routeType = Route.COMBINED;
			break;
		}
		}

		RoadInformation road = new RoadInformation(transportType, routeType,
				Integer.parseInt(datas[12]));

		VoucherToTreatment voucher = new VoucherToTreatment(
				Integer.parseInt(datas[1]), datas[2], datas[3], residence,
				road, Integer.parseInt(datas[13]));
		vouchers.add(voucher);
	}

	public void addVoucher(String voucherType, String data)
			throws ParseException {
		BasicConfigurator.configure();
		try {
			String datas[] = data.split("|");
			switch (voucherType) {
			case "VoucherToCruise": {
				addVoucherToCruise(datas);
				break;
			}
			case "VoucherToExcursion": {
				addVoucherToExcursion(datas);
				break;
			}
			case "VoucherToRelax": {
				addVoucherToRelax(datas);
				break;
			}
			case "VoucherToShopping": {
				addVoucherToShopping(datas);
				break;
			}
			case "VoucherToTreatment": {
				addVoucherToTreatment(datas);
				break;
			}
			default: {
				logger.error("No vouchers type");
			}
			}
		} catch (ParseException exception) {
			logger.error(exception.getMessage());
		}
	}

	public VouchersList() {
		super();
	}

}
